home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / sipp / libsipp / xalloca.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-14  |  5.3 KB  |  201 lines

  1. #ifdef HAVE_NO_ALLOCA
  2.  
  3. /*
  4.     alloca -- (mostly) portable public-domain implementation -- D A Gwyn
  5.  
  6.     last edit:    86/05/30    rms
  7.        include config.h, since on VMS it renames some symbols.
  8.        Use xmalloc instead of malloc.
  9.  
  10.     This implementation of the PWB library alloca() function,
  11.     which is used to allocate space off the run-time stack so
  12.     that it is automatically reclaimed upon procedure exit, 
  13.     was inspired by discussions with J. Q. Johnson of Cornell.
  14.  
  15.     It should work under any C implementation that uses an
  16.     actual procedure stack (as opposed to a linked list of
  17.     frames).  There are some preprocessor constants that can
  18.     be defined when compiling for your specific system, for
  19.     improved efficiency; however, the defaults should be okay.
  20.  
  21.     The general concept of this implementation is to keep
  22.     track of all alloca()-allocated blocks, and reclaim any
  23.     that are found to be deeper in the stack than the current
  24.     invocation.  This heuristic does not reclaim storage as
  25.     soon as it becomes invalid, but it will do so eventually.
  26.  
  27.     As a special case, alloca(0) reclaims storage without
  28.     allocating any.  It is a good idea to use alloca(0) in
  29.     your main control loop, etc. to force garbage collection.
  30. */
  31. #ifndef lint
  32. static char    SCCSid[] = "@(#)alloca.c    1.1";    /* for the "what" utility */
  33. #endif
  34.  
  35. #include "xalloca.h"
  36.  
  37. extern void    free();
  38.  
  39. /* ================================================================ */
  40. /*      This is entered from general.h in the GNU shell bash.       */
  41.  
  42.  
  43. #include <stdio.h>
  44.  
  45. /* **************************************************************** */
  46. /*                                    */
  47. /*           Memory Allocation and Deallocation.            */
  48. /*                                    */
  49. /* **************************************************************** */
  50.  
  51. static char *
  52. xmalloc (size)
  53.      int size;
  54. {
  55.     char *temp = (char *)malloc (size);
  56.     
  57.     if (!temp) {
  58.         fprintf (stderr, "Out of virtual memory!");
  59.         exit(1);
  60.     }
  61.  
  62.     return (temp);
  63. }
  64.  
  65.  
  66. /* ================================================================ */
  67.  
  68. /*
  69.     Define STACK_DIRECTION if you know the direction of stack
  70.     growth for your system; otherwise it will be automatically
  71.     deduced at run-time.
  72.  
  73.     STACK_DIRECTION > 0 => grows toward higher addresses
  74.     STACK_DIRECTION < 0 => grows toward lower addresses
  75.     STACK_DIRECTION = 0 => direction of growth unknown
  76. */
  77.  
  78. #ifndef STACK_DIRECTION
  79. #define    STACK_DIRECTION    0        /* direction unknown */
  80. #endif
  81.  
  82. #if STACK_DIRECTION != 0
  83.  
  84. #define    STACK_DIR    STACK_DIRECTION    /* known at compile-time */
  85.  
  86. #else    /* STACK_DIRECTION == 0; need run-time code */
  87.  
  88. static int    stack_dir;        /* 1 or -1 once known */
  89. #define    STACK_DIR    stack_dir
  90.  
  91. static void
  92. find_stack_direction (/* void */)
  93. {
  94.   static char    *addr = NULL;    /* address of first
  95.                    `dummy', once known */
  96.   auto char    dummy;        /* to get stack address */
  97.  
  98.   if (addr == NULL)
  99.     {                /* initial entry */
  100.       addr = &dummy;
  101.  
  102.       find_stack_direction ();    /* recurse once */
  103.     }
  104.   else                /* second entry */
  105.     if (&dummy > addr)
  106.       stack_dir = 1;        /* stack grew upward */
  107.     else
  108.       stack_dir = -1;        /* stack grew downward */
  109. }
  110.  
  111. #endif    /* STACK_DIRECTION == 0 */
  112.  
  113. /*
  114.     An "alloca header" is used to:
  115.     (a) chain together all alloca()ed blocks;
  116.     (b) keep track of stack depth.
  117.  
  118.     It is very important that sizeof(header) agree with malloc()
  119.     alignment chunk size.  The following default should work okay.
  120. */
  121.  
  122. #ifndef    ALIGN_SIZE
  123. #define    ALIGN_SIZE    sizeof(double)
  124. #endif
  125.  
  126. typedef union hdr
  127. {
  128.   char    align[ALIGN_SIZE];    /* to force sizeof(header) */
  129.   struct
  130.     {
  131.       union hdr *next;        /* for chaining headers */
  132.       char *deep;        /* for stack depth measure */
  133.     } h;
  134. } header;
  135.  
  136. /*
  137.     alloca( size ) returns a pointer to at least `size' bytes of
  138.     storage which will be automatically reclaimed upon exit from
  139.     the procedure that called alloca().  Originally, this space
  140.     was supposed to be taken from the current stack frame of the
  141.     caller, but that method cannot be made to work for some
  142.     implementations of C, for example under Gould's UTX/32.
  143. */
  144.  
  145. static header *last_alloca_header = NULL; /* -> last alloca header */
  146.  
  147. pointer
  148. alloca (size)            /* returns pointer to storage */
  149.      unsigned    size;        /* # bytes to allocate */
  150. {
  151.   auto char    probe;        /* probes stack depth: */
  152.   register char    *depth = &probe;
  153.  
  154. #if STACK_DIRECTION == 0
  155.   if (STACK_DIR == 0)        /* unknown growth direction */
  156.     find_stack_direction ();
  157. #endif
  158.  
  159.   /* Reclaim garbage, defined as all alloca()ed storage that
  160.      was allocated from deeper in the stack than currently. */
  161.   {
  162.     register header    *hp;    /* traverses linked list */
  163.  
  164.     for (hp = last_alloca_header; hp != NULL;)
  165.       if (STACK_DIR > 0 && hp->h.deep > depth
  166.       || STACK_DIR < 0 && hp->h.deep < depth)
  167.     {
  168.       register header    *np = hp->h.next;
  169.  
  170.       free ((pointer) hp);    /* collect garbage */
  171.  
  172.       hp = np;        /* -> next header */
  173.     }
  174.       else
  175.     break;            /* rest are not deeper */
  176.  
  177.     last_alloca_header = hp;    /* -> last valid storage */
  178.   }
  179.  
  180.   if (size == 0)
  181.     return NULL;        /* no allocation required */
  182.  
  183.   /* Allocate combined header + user data storage. */
  184.  
  185.   {
  186.     register pointer    new = xmalloc (sizeof (header) + size);
  187.     /* address of header */
  188.  
  189.     ((header *)new)->h.next = last_alloca_header;
  190.     ((header *)new)->h.deep = depth;
  191.  
  192.     last_alloca_header = (header *)new;
  193.  
  194.     /* User storage begins just after header. */
  195.  
  196.     return (pointer)((char *)new + sizeof(header));
  197.   }
  198. }
  199.  
  200. #endif  /* HAVE_NO_ALLOCA */
  201.